define(["jquery","bbcare","comm/datatable/datatable","bld/app/course/courseEdit.ui"],function($,$bb,$datatable,$ui) {
    var servers = {};
    var URL = $bb.baseUrl("blood");

    /**
     * 修改疗程名称
     */
    servers.ajaxModifyCourseName = function(params,callback,courseName) {
        $bb.ajax({
            url: URL + "course/modifyCourseName",
            data: JSON.stringify({
                data: params
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data,courseName);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "修改疗程名称出错！");
                }
            }
        });
    }

    /**
     * 发布疗程
     */
    servers.ajaxReleaseCourse = function(params,callback) {
        $bb.ajax({
            url: URL + "course/releaseCourse",
            data: JSON.stringify({
                data: params
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "发布疗程出错！");
                }
            }
        });
    }

    /**
     * 查询疗程详情
     */
    servers.ajaxViewCourse = function(params,callback) {
        $bb.ajax({
            url: URL + "course/viewCourse",
            data: JSON.stringify({
                data: params
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "查询疗程详情出错！");
                }
            }
        });
    }

    /**
     * 处理疗程数据
     */
    servers.handleData = function(data){
        var arr = [];
        for (var key in data.courseDataInfos) {
            var obj = {};
            obj.day = key;
            obj.medicineList = [];
            for (var item in data.courseDataInfos[key]) {
                var obj1 = {};
                obj1.medicineId = item;
                obj1.value = data.courseDataInfos[key][item];
                obj.medicineList.push(obj1);
            }
            arr.push(obj);
        }
        for (var i = 0; i < arr.length; i++) {
            arr[i].list = [];
            for (var k = 0; k < data.medicines.length; k++) {
                for (var j = 0; j < arr[i].medicineList.length; j++) {
                    if (data.medicines[k].medicineId == arr[i].medicineList[j].medicineId) {
                        var obj = {};
                        obj.medicineId = data.medicines[k].medicineId;
                        obj.value = arr[i].medicineList[j].value;
                        obj.frequencyTime = data.medicines[k].frequencyTime;
                        var reg = /^(\d+)(\.\d{1,2})?$/;
                        obj.valueFrequencyTime = reg.test(obj.value / obj.frequencyTime) ? (obj.value / obj.frequencyTime) : (obj.value / obj.frequencyTime).toFixed(3);
                        obj.flag = obj.frequencyTime == 1 ? false : true;
                        arr[i].list.push(obj);
                        break;
                    }
                }
            }
        }
        return arr;
    }

    /**
     * 根据medicineId获取药品信息
     */
    servers.getCurrMedicine = function(medicines,medicineId){
        for (var i = 0; i < medicines.length; i++) {
            if( medicines[i].medicineId == medicineId ){
                return medicines[i];
            }
        }
    }

    /**
     * 根据medSkuId获取药品信息
     */
    servers.getCurrMedicineByMedSkuId = function(medicines,medSkuId){
        for (var i = 0; i < medicines.length; i++) {
            if( medicines[i].medSkuId == medSkuId ){
                return medicines[i];
            }
        }
    }

    /**
     * 药品单位查询
     */
    servers.ajaxQueryUnit = function(callback,datas) {
        $bb.ajax({
            url: URL + "medUnitDict/queryAllMedUnitDict",
            success: function(data) {
                if (data.success) {
                    callback(data.data,datas);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "药品单位查询出错！");
                }
            }
        });
    }

    /**
     * 获取药品大类
     */
    servers.ajaxMedicineBigClass = function(callback,datas) {
        $bb.ajax({
            url: URL + "medicineClass/getAllMedicineClass",
            success: function(data) {
                if (data.success) {
                    callback(data.data,datas);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "获取药品大类出错！");
                }
            }
        });
    }

    /**
     * 获取药品小类
     */
    servers.ajaxMedicineSmallClass = function(params,callback,datas) {
        $bb.ajax({
            url: URL + "medicineInfo/getMedCatalogInfos",
            data: JSON.stringify({
                data: params
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data,datas);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "获取药品小类出错！");
                }
            }
        });
    }
    /**
     * 根据药品名称查询
     */
    servers.ajaxSelect = function(params,callback,datas) {
        $bb.ajax({
            url: URL + "medicineInfo/queryMedSkuInfoByName",
            data: JSON.stringify({
                data: params
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data,datas);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "根据药品名称查询药品出错！");
                }
            }
        });
    }
    /**
     * 给疗程库中的疗程添加药品
     */
    servers.ajaxAdd = function(params,callback,datas) {
        $bb.ajax({
            url: URL + "course/addMedicine",
            data: JSON.stringify({
                data: params
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data,datas);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "给疗程库中的疗程添加药品出错！");
                }
            }
        });
    }

    /**
     * 删除疗程库下某个疗程中的某种药品
     */
    servers.ajaxRemoveCourseMedicine = function(params,callback) {
        $bb.ajax({
            url: URL + "course/reduceMedicine",
            data: JSON.stringify({
                data: params
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "删除疗程库下某个疗程中的某种药品出错！");
                }
            }
        });
    }

    /**
     * 移动药物
     */
    servers.ajaxMoveMedicine = function(params,callback) {
        $bb.ajax({
            url: URL + "course/moveMedicine",
            data: JSON.stringify({
                data: params
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "移动药物出错！");
                }
            }
        });
    }

    /**
     * 修改药品
     */
    servers.ajaxUpdateCourseMedicine = function(params,callback,datas) {
        $bb.ajax({
            url: URL + "course/updateCourseMedicine",
            data: JSON.stringify({
                data: params
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data,datas);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "修改药品出错！");
                }
            }
        });
    }

    /**
     * 给疗程添加天数
     */
    servers.ajaxIncreaseDay = function(params,callback) {
        $bb.ajax({
            url: URL + "course/increaseDay",
            data: JSON.stringify({
                data: params
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "给疗程添加天数出错！");
                }
            }
        });
    }

    /**
     * 删除疗程中的某一天
     */
    servers.ajaxReduceDay = function(params,callback) {
        $bb.ajax({
            url: URL + "course/reduceDay",
            data: JSON.stringify({
                data: params
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "删除疗程中的某一天出错！");
                }
            }
        });
    }

    /**
     * 给疗程库中的疗程调整剂量
     */
    servers.ajaxChangeMedicineDose = function(params,callback) {
        $bb.ajax({
            url: URL + "course/changeMedicineDose",
            data: JSON.stringify({
                data: params
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "调整剂量出错！");
                }
            }
        });
    }
    
    return servers;
})